package com.dw.etl.core.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.LayTree;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.vo.Tree;
import com.dw.etl.core.bean.model.*;
import com.dw.etl.core.mapper.*;
import com.dw.etl.core.api.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author GuoPeng
 * @Title: UserServiceImpl
 * @Description: TODO
 * @date 2019/5/11 10:22
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {


    @Resource
    private SysUsersMapper sysUsersMapper;


    @Resource
    private SysUsersRolesMapper sysUsersRolesMapper;

    @Resource
    private SysRolesPermissionsMapper sysRolesPermissionsMapper;

    @Resource
    private SysTagMapper sysTagMapper;

    @Resource
    private SysRolesMapper sysRolesMapper;


    @Resource
    private  SysPermissionsMapper sysPermissionsMapper;

    @Resource
    private  SysTagPermissionsMapper sysTagPermissionsMapper;

    @Override
    public LayMsg getuserList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Page<SysUsers> selectall = (Page) sysUsersMapper.selectall();
        return LayMsg.success().addCount(selectall.getTotal()).addData(selectall.getResult());
    }

    @Override
    public Msg updateuser(SysUsers user) {
        int i = sysUsersMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    @Override
    public Msg userLock(SysUsers user) {
        int i = sysUsersMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    @Override
    public Msg delectuser(Integer id) {
        int i = sysUsersMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    @Override
    public LayTree selectTree(Integer id) {
        SysUsers sysUsers = sysUsersMapper.selectByPrimaryKey(id);
        Tree difaTree = Tree.createDifaTree();
        difaTree.setId(sysUsers.getId());
        difaTree.setLabel(sysUsers.getUsername());

        HashMap map = new HashMap();
        map.put("user_id", sysUsers.getId());
        List<SysUsersRoles> sysUsersRoles = sysUsersRolesMapper.selectbyUseridAndRoleid(map);


        List<Tree> arrayList1 = new ArrayList();
        for (int i = 0; i < sysUsersRoles.size(); i++) {
            SysRoles sysRoles = sysRolesMapper.selectByPrimaryKey(sysUsersRoles.get(i).getRoleId());
            Tree difaTree2 = Tree.createDifaTree();
            difaTree2.setLabel(sysRoles.getRole());
            difaTree2.setId(sysRoles.getId());

            arrayList1.add(difaTree2);
            difaTree.setChildren(arrayList1);

            List<Tree> arrayList2 = new ArrayList();
            HashMap pram2 = new HashMap();
            pram2.put("role_id", sysRoles.getId());
            List<SysRolesPermissions> sysRolesPermissions = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(pram2);
            for(int m=0;m<sysRolesPermissions.size();m++ ){
                SysPermissions sysPermissions = sysPermissionsMapper.selectByPrimaryKey(sysRolesPermissions.get(m).getPermissionId());

                Tree difaTree3 = Tree.createDifaTree();
                difaTree3.setId(sysPermissions.getId());
                difaTree3.setLabel(sysPermissions.getPermission());
                arrayList2.add(difaTree3);
                difaTree2.setChildren(arrayList2);


                List<Tree> arrayList3 = new ArrayList();
                HashMap pram3 = new HashMap();
                pram3.put("permissions_id", sysPermissions.getId());
                List<SysTag> sysTags=new ArrayList<>();
                List<SysTagPermissions> sysTagPermissions = sysTagPermissionsMapper.selectbypridAndtagId(pram3);

                for (SysTagPermissions sysTagPermission : sysTagPermissions) {
                    SysTag sysTag = sysTagMapper.selectByPrimaryKey(sysTagPermission.getTagId());
                    sysTags.add(sysTag);
                }
                for(int n=0;n<sysTags.size();n++){
                    Tree difaTree4 = Tree.createDifaTree();
                    difaTree4.setId(sysTags.get(n).getId());
                    difaTree4.setLabel(sysTags.get(n).getUrl());
                    arrayList3.add(difaTree4);
                    difaTree3.setChildren(arrayList3);
                }
            }

        }

        difaTree.setChildren(arrayList1);

        ArrayList<Tree> lst = new ArrayList();
        lst.add(difaTree);
        return LayTree.createLayTree().addLayTree(lst);
    }

    @Override
    public LayTree SelectreeAll() {
        ArrayList<Tree> trees = new ArrayList<>();

        List<SysUsers> selectall = sysUsersMapper.selectall();
        for(int g=0;g< selectall.size();g++){
            SysUsers sysUsers = sysUsersMapper.selectByPrimaryKey(selectall.get(g).getId());
            Tree difaTree = Tree.createDifaTree();
            difaTree.setId(sysUsers.getId());
            difaTree.setLabel(sysUsers.getUsername());

            trees.add(difaTree);

            HashMap map = new HashMap();
            map.put("user_id", sysUsers.getId());
            List<SysUsersRoles> sysUsersRoles = sysUsersRolesMapper.selectbyUseridAndRoleid(map);


            List<Tree> arrayList1 = new ArrayList();
            for (int i = 0; i < sysUsersRoles.size(); i++) {
                SysRoles sysRoles = sysRolesMapper.selectByPrimaryKey(sysUsersRoles.get(i).getRoleId());
                Tree difaTree2 = Tree.createDifaTree();
                difaTree2.setLabel(sysRoles.getRole());
                difaTree2.setId(sysRoles.getId());

                arrayList1.add(difaTree2);
                difaTree.setChildren(arrayList1);

                List<Tree> arrayList2 = new ArrayList();
                HashMap pram2 = new HashMap();
                pram2.put("role_id", sysRoles.getId());
                List<SysRolesPermissions> sysRolesPermissions = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(pram2);
                for(int m=0;m<sysRolesPermissions.size();m++ ){
                    SysPermissions sysPermissions = sysPermissionsMapper.selectByPrimaryKey(sysRolesPermissions.get(m).getPermissionId());

                    Tree difaTree3 = Tree.createDifaTree();
                    difaTree3.setId(sysPermissions.getId());
                    difaTree3.setLabel(sysPermissions.getPermission());
                    arrayList2.add(difaTree3);
                    difaTree2.setChildren(arrayList2);


                    List<Tree> arrayList3 = new ArrayList();
                    List<SysTag> sysTags = new ArrayList();
                    HashMap pram3 = new HashMap();
                    pram2.put("role_id", sysRoles.getId());
                    List<SysTagPermissions> sysTagPermissions = sysTagPermissionsMapper.selectbypridAndtagId(pram3);
                    for (SysTagPermissions sysTagPermission : sysTagPermissions) {
                        SysTag sysTag = sysTagMapper.selectByPrimaryKey(sysTagPermission.getTagId());
                        sysTags.add(sysTag);
                    }

                    for(int n=0;n<sysTags.size();n++){
                        Tree difaTree4 = Tree.createDifaTree();
                        difaTree4.setId(sysTags.get(n).getId());
                        difaTree4.setLabel(sysTags.get(n).getUrl());
                        arrayList3.add(difaTree4);
                        difaTree3.setChildren(arrayList3);
                    }
                }

            }

        }

        return LayTree.createLayTree().addLayTree(trees);
    }



}
